<?php

require('../config.php');
require('./messages.php');

// Script to test the server functions.

$username = 'test';
$password = 'pass';
$sectorName = 'Sol';
$newSectorName = 'Sector';
$sectorData = 'testsectordata';
$pricelistName = 'TestPricelist';
$newPricelistName = 'noob pricelist';
$pricelistData = 'testpricelistdata';
$sectorCodeName = 'PJ3373';
$sectorCodeData = 'bbbbbbeeebbbbbbbeeebbeeebbbebbeefeeeeeeeefeeeeeeeeeeebbbbbbb|9X5';

if(isset($_GET['debug']))
  define('debug', true);

else
  define('debug', false);

function QueryString($arr)
{
  $query = array();

  foreach($arr as $key=>$value)
  {
    $query[] = urlencode($key).'='.urlencode($value);
  }

  return implode("&", $query);
}

function PrintResult($test, $name)
{
  if($test)
    print $name.'() -> <span style="color: green;">PASS</span> <br />';

  else
    print $name.'() -> <span style="color: red;">FAIL</span> <br />';
}

function SendData($action, $data=array())
{
  global $username;
  global $password;
  $data = QueryString($data);

  $conn = curl_init();

  $query = QueryString(array('action'=>$action, 'protocol'=>1, 'username'=>$username, 'password'=>$password));
  $url = 'http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['REQUEST_URI']).'/index.php?'.$query;

  curl_setopt($conn, CURLOPT_URL, $url);
  curl_setopt($conn, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($conn, CURLOPT_POST, 1);
  curl_setopt($conn, CURLOPT_POSTFIELDS, $data);

  $result = curl_exec($conn);
  curl_close($conn);

  if(debug)
  {
    print '<hr>';
    print "Request URL: $url <br />";
    print "Request: $data <br />";
    print "Result: $result<br />";
    print 'Result Array: <pre>';
    print_r(json_decode($result, true));
    print '</pre>';
  }

  $result = json_decode($result, true);

  return $result;
}

function TestRegister()
{
  $result = SendData(MsgRegister);

  PrintResult($result['Status'] == STATUS_OK, 'TestRegister');
}

function TestLogin()
{
  $result = SendData(MsgLogin);

  PrintResult($result['Status'] == STATUS_OK, 'TestLogin');
}

function TestGetSettings()
{
  $result = SendData(MsgGetSettings);

  PrintResult( $result['MaxSectors'] == MAX_SAVED_SECTORS &&
               $result['MaxPricelists'] == MAX_SAVED_PRICELISTS &&
               $result['Time'] <= gmmktime(), 'TestGetSettings' );
}

function TestAddSector()
{
  global $sectorName;
  global $sectorData;

  $result = SendData(MsgAddSector, array('name'=>$sectorName, 'data'=>$sectorData));

  PrintResult($result['Status'] == STATUS_OK, 'TestAddSector');
}

function TestAddPricelist()
{
  global $pricelistName;
  global $pricelistData;

  $result = SendData(MsgAddPricelist, array('name'=>$pricelistName, 'data'=>$pricelistData));

  PrintResult($result['Status'] == STATUS_OK, 'TestAddPricelist');
}

function TestGetSectors()
{
  global $sectorName;

  $result = SendData(MsgGetSectors);

  PrintResult($result['Sectors'][0] == $sectorName, 'TestGetSectors');
}

function TestGetPricelists()
{
  global $pricelistName;

  $result = SendData(MsgGetPricelists);

  PrintResult($result['Pricelists'][0] == $pricelistName, 'TestGetPricelists');
}

function TestGetSector()
{
  global $sectorName;
  global $sectorData;

  $result = SendData(MsgGetSector, array('name'=>$sectorName));

  PrintResult($result['Code'] == $sectorData, 'TestGetSector');
}

function TestGetPricelist()
{
  global $pricelistName;
  global $pricelistData;

  $result = SendData(MsgGetPricelist, array('name'=>$pricelistName));

  PrintResult($result['Code'] == $pricelistData, 'TestGetPricelist');
}

function TestUpdateSector()
{
  global $sectorName;

  $result = SendData(MsgUpdateSector, array('name'=>$sectorName, 'data'=>'noobysector'));

  PrintResult($result['Status'] == STATUS_OK, 'TestUpdateSector');

  $result = SendData(MsgGetSector, array('name'=>$sectorName));

  PrintResult($result['Code'] == 'noobysector', 'TestUpdateSector');
}

function TestUpdatePricelist()
{
  global $pricelistName;

  $result = SendData(MsgUpdatePricelist, array('name'=>$pricelistName, 'data'=>'noobypricelist'));

  PrintResult($result['Status'] == STATUS_OK, 'TestUpdatePricelist');

  $result = SendData(MsgGetPricelist, array('name'=>$pricelistName));

  PrintResult($result['Code'] == 'noobypricelist', 'TestUpdatePricelist');
}

function TestRenameSector()
{
  global $sectorName;
  global $newSectorName;

  $result = SendData(MsgRenameSector, array('oldName'=>$sectorName, 'newName'=>$newSectorName));

  PrintResult($result['Status'] == STATUS_OK, 'TestRenameSector');

  $result = SendData(MsgRenameSector, array('oldName'=>$newSectorName, 'newName'=>$sectorName));

  PrintResult($result['Status'] == STATUS_OK, 'TestRenameSector');
}

function TestRenamePricelist()
{
  global $pricelistName;
  global $newPricelistName;

  $result = SendData(MsgRenamePricelist, array('oldName'=>$pricelistName, 'newName'=>$newPricelistName));

  PrintResult($result['Status'] == STATUS_OK, 'TestRenamePricelist');

  $result = SendData(MsgRenamePricelist, array('oldName'=>$newPricelistName, 'newName'=>$pricelistName));

  PrintResult($result['Status'] == STATUS_OK, 'TestRenamePricelist');
}

function TestGetSectorCode()
{
  global $sectorCodeName;
  global $sectorCodeData;

  $result = SendData(MsgGetSectorCode, array('name'=>$sectorCodeName));

  PrintResult($result['Code'] == $sectorCodeData, 'TestGetSectorCode');
}

function TestDeleteSector()
{
  global $sectorName;

  $result = SendData(MsgDeleteSector, array('name'=>$sectorName));

  PrintResult($result['Status'] == STATUS_OK, 'TestDeleteSector');
}

function TestDeletePricelist()
{
  global $pricelistName;

  $result = SendData(MsgDeletePricelist, array('name'=>$pricelistName));

  PrintResult($result['Status'] == STATUS_OK, 'TestDeletePricelist');
}

function TestDeleteUser()
{
  $result = SendData(MsgDeleteUser);

  PrintResult($result['Status'] == STATUS_OK, 'TestDeleteUser');
}

function FullTest()
{
  TestRegister();
  TestLogin();
  TestGetSettings();
  TestAddSector();
  TestAddPricelist();
  TestGetSectors();
  TestGetPricelists();
  TestGetSector();
  TestGetPricelist();
  TestUpdateSector();
  TestUpdatePricelist();
  TestRenameSector();
  TestRenamePricelist();
  TestGetSectorCode();
  TestDeleteSector();
  TestDeletePricelist();
  TestDeleteUser();
}

function StructureTest()
{
  TestRegister();
  TestLogin();
  TestAddSector();
  TestAddPricelist();
  TestGetSectors();
  TestGetPricelists();
  TestGetSector();
  TestGetPricelist();
  TestUpdateSector();
  TestUpdatePricelist();
}

//StructureTest();
FullTest();

?>